﻿using System.Reflection;
using System.Windows.Forms;
using CnSharp.SSMS.Demo.Controls;
using CnSharp.SSMS.AddIn.Framework;
using CnSharp.SSMS.AddIn.Util;

namespace CnSharp.SSMS.Demo.Commands
{
    public class FindObjectCommand : ICommand
    {
        #region ICommand Members

        public void Execute()
        {
            var dbNode = ObjectExplorerHelper.GetContext();
            if (dbNode == null || string.IsNullOrEmpty(dbNode.ConnectionString) || string.IsNullOrEmpty(dbNode.Database))
            {
                MessageBox.Show("Select a database in the Object Explorer first", Common.ProductShortName,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //var ssmsWindow = (Windows2)ServiceCache.ExtensibilityModel.Application.Windows;
            //object controlObject = null;
            //var findWindow = ssmsWindow.CreateToolWindow2(addInInstance, GetType().Assembly.Location, typeof(FindControl).FullName, "Find Object", "{B44B4185-A5AA-45c3-BDEB-1307553606ED}", ref controlObject);
            //findWindow.Visible = true;
            var window = WindowHelper.FindWindowByGuid(Guids.FindObjectView);
            if (window != null)
            {
                window.Activate();
                return;
            }
            var win = new WindowInfo
                {
                    AssemblyLocation = Assembly.GetExecutingAssembly().Location,
                    ClassName = typeof (FindControl).FullName,
                    Caption = "Find Object - " + dbNode.Server,
                    Width = 450,
                    Height = 350,
                    Guid = Guids.FindObjectView
                };
            WindowHelper.AddWindow(win);
        }

        #endregion
    }
}